package fr.kirin.logger;

import java.util.HashMap;

import fr.kirin.logger.i18n.I18N;
import fr.kirin.logger.model.DefaultLoggerTreeModel;
import fr.kirin.logger.model.Level;
import fr.kirin.logger.model.LoggerTreeModel;
import fr.kirin.logger.model.gui.GraphicLoggerGUI;


/**
 * 
 * Main class of the logger.
 * this class provide a logger factory 
 * @author kirin
 * 
 *
 */
public class GraphicLogger {


    private  LoggerTreeModel model;
    private String name;
    private GraphicLoggerGUI gui;
    private static  GraphicLogger defaultInstance;
    private static HashMap<String, GraphicLogger> loggerMap = new HashMap<String, GraphicLogger>();



    /**
     * 
     * @return the default logger instance.
     */
    public static GraphicLogger getInstance(){
        if(defaultInstance == null){
            DefaultLoggerTreeModel defaultLoggerTreeModel = new DefaultLoggerTreeModel("logs");
            defaultInstance = new GraphicLogger(defaultLoggerTreeModel,"default",new GraphicLoggerGUI(defaultLoggerTreeModel));
        }

        return defaultInstance;
    }

    /** get a particular instance of the logger identified by its name
     * 
     * @param name name of the instance
     * @return a particular instance of the graphic logger or null.
     */
    public static GraphicLogger getInstance(String name){
        return loggerMap.get(name);
    }


    /**Create a new instance of a particular logger.
     * If instance already exist it will be returned instead of a new logger.
     * 
     * @param name name of the logger
     * @return the new graphic logger.
     */
    public static GraphicLogger createInstance(String name){
        if(!loggerMap.containsKey(name)){
            GraphicLogger logger = new GraphicLogger(null,name,null);
            loggerMap.put(name,logger);
            return logger;
        }
        return loggerMap.get(name);
    }



    private GraphicLogger(LoggerTreeModel model,String name,GraphicLoggerGUI gui){
        this.model = model;
        this.name = name;
        this.gui = gui;
    }

    
    private GraphicLogger(){
    }

    /**
     * 
     * @return the treeModel associated with the graphic logger
     */
    public LoggerTreeModel getModel() {
        return model;
    }

    /**set the tree model for this logger.
     * 
     * @param model the tree model to use.
     */
    public void setModel(LoggerTreeModel model) {
        this.model = model;
    }

    /**
     * 
     * @return the name of the logger.
     */
    public String getName() {
        return name;
    }


    /**log an event into the logger.
     * 
     * @param category category of the event.
     * @param value event value.
     * @param level event level.
     * @param comment comment on the event.
     */
    public void log(String category, String value, Level level, String comment){
        if(model == null){
            return;

        }
        model.addItem(category, value,comment, level);
    }

    /**log an event without comment
     * 
     * @param category category of the event.
     * @param value event value.
     * @param level event level.
     */
    public void log(String category, String value, Level level){
        log(category,value,level,"");
    }

    /**
     * 
     * @return the GUI associated with the current logger.
     */
    public GraphicLoggerGUI getGui()
    {
        return gui;
    }

    /**set the GUI for this logger
     * 
     * @param gui the GUI to use.
     */
    public void setGui(GraphicLoggerGUI gui)
    {
        this.gui = gui;
    }
    
    public void setLocale(String language,String country){
        I18N.setLocale(language,country);
    }




}
